//
//  RoundedPentagonView.m
//  yuikemall
//
//  Created by Gang.Wang on 12-6-29.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

/*
 等边五角星
     角度：
       尖为 36度
       凹进去的地方为 108度
       每两个顶点 和中心 之间为角度为 （ 180 - 36 ）/ 2 = 72
       也要理解为 360 / 5 = 72
 
  所以，五个定点就以 上方的中心点为第一个点，然后依次旋转 72 度，就得到了 5个顶点，
 
  然后再计算两条直接的相交点，就得出了每个一个凹点的坐标，这样十个点都有了，开始玩吧 :)
 
 */
#import "RoundedPentagonView.h"
#import "header.h"


#define _PENTAGON_                      0.75f

@interface RoundedPentagonView ()
{
    CGContextRef mCtx_;
    
    NSMutableArray * outskirtsPoints_;
    NSMutableArray * centerskirtPoints_;
    
}

@property (nonatomic, assign) CGContextRef mCtx;

@property (nonatomic, retain) NSMutableArray * outskirtsPoints;
@property (nonatomic, retain) NSMutableArray * centerskirtPoints;

- (void) privateInit;

- (void) drawPentagon;

- (void) calcPoints;
@end

@implementation RoundedPentagonView
@synthesize mCtx = mCtx_;

@synthesize outskirtsPoints = outskirtsPoints_;
@synthesize centerskirtPoints = centerskirtPoints_;

- (void) dealloc
{
    YRelease(outskirtsPoints_)
    YRelease(centerskirtPoints_)
    
    [super dealloc];
}


#pragma mark - Public Method
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}

- (CGContextRef) mCtx
{
    if (!mCtx_) {
        mCtx_ = UIGraphicsGetCurrentContext();
    }
    
    return mCtx_;
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self calcPoints];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextSaveGState(self.mCtx);
    [self drawPentagon];
}



#pragma mark - Private Method
- (void) privateInit
{
    if (self.bInited) {
        return;
    }
    
    
    [self reAdjustLayout];
    self.bInited = TRUE;
}

- (void) drawPentagon
{   
    CGFloat radius = 0;    
    BOOL bFirstPoint = TRUE;    
    for (int i = 0; i < 5; i++) 
    {        
        CGPoint point = [(NSValue *)[self.centerskirtPoints objectAtIndex:i] CGPointValue];
        CGPoint nextPoint = [(NSValue *)[self.outskirtsPoints objectAtIndex:i] CGPointValue];
        if (bFirstPoint) 
        {            
            CGContextMoveToPoint(self.mCtx, point.x, point.y);
            
            CGContextAddLineToPoint(self.mCtx, nextPoint.x, nextPoint.y); 
            bFirstPoint = FALSE;
    
            continue;
        }
            
        CGContextAddArcToPoint(self.mCtx, point.x, point.y, nextPoint.x, nextPoint.y, radius);        
        CGContextAddLineToPoint(self.mCtx, nextPoint.x, nextPoint.y);  
 
        CGContextSaveGState(self.mCtx);
        CGContextRestoreGState(self.mCtx);         
    }
    
    CGContextClosePath(self.mCtx); 
    CGContextClip(self.mCtx);    
    [self.image drawInRect:self.bounds];
    CGContextSaveGState(self.mCtx);
    CGContextRestoreGState(self.mCtx);      
}

- (void) calcPoints
{
    NSMutableArray * temp_points1 = [[NSMutableArray alloc] initWithCapacity:5];
    self.outskirtsPoints = temp_points1;
    YRelease(temp_points1);
    
    NSMutableArray * temp_points2 = [[NSMutableArray alloc] initWithCapacity:5];
    self.centerskirtPoints = temp_points2;
    YRelease(temp_points2);
    
    CGFloat rectWidth = self.width > self.height ? self.height : self.width;
    
    CGRect rRect = CGRectMake((self.width - rectWidth) / 2, 
                              (self.height - rectWidth ) / 2, 
                              rectWidth, 
                              rectWidth);
    
    
    CGPoint outskirtsPoint1 = CGPointMake(rRect.size.width / 2, rRect.origin.y);
    [self.outskirtsPoints addObject:[NSValue valueWithCGPoint:outskirtsPoint1]];
    
    float point1Angle = 360 / 5;
    float radina = [YMath angle2Radina:point1Angle];
    
    for (int i = 0; i < 4; i++) 
    {
        
        CGPoint lastPoint1 = [(NSValue *)[self.outskirtsPoints lastObject] CGPointValue];
        CGPoint newPoint = [YMath rotatePoint:lastPoint1 
                                       radian:radina 
                              withRotatePoint:[self boundsCenter]];
        [self.outskirtsPoints addObject:[NSValue valueWithCGPoint:newPoint]];
    }
    
    if ([self.outskirtsPoints count] != 5) {
        return;
    }
    
    
    CGPoint centerskirtPoint1 = [YMath crossPoint:[(NSValue *)[self.outskirtsPoints objectAtIndex:0] CGPointValue] 
                                           point2:[(NSValue *)[self.outskirtsPoints objectAtIndex:3] CGPointValue] 
                                           point3:[(NSValue *)[self.outskirtsPoints objectAtIndex:1] CGPointValue] 
                                           point4:[(NSValue *)[self.outskirtsPoints objectAtIndex:4] CGPointValue]];
    
    centerskirtPoint1.x = centerskirtPoint1.x * _PENTAGON_;
    centerskirtPoint1.y = centerskirtPoint1.y * _PENTAGON_;
    
    [self.centerskirtPoints addObject:[NSValue valueWithCGPoint:centerskirtPoint1]];
    
    for (int i = 0; i < 4; i++) 
    {
        CGPoint lastPoint1 = [(NSValue *)[self.centerskirtPoints lastObject] CGPointValue];
        CGPoint newPoint = [YMath rotatePoint:lastPoint1 
                                       radian:radina 
                              withRotatePoint:[self boundsCenter]];
        [self.centerskirtPoints addObject:[NSValue valueWithCGPoint:newPoint]];

    }
    
}



@end
